package com.kantboot.business.gc.repository;

import com.kantboot.business.gc.domain.dto.BusGcGameSearchDTO;
import com.kantboot.business.gc.domain.vo.BusGcGameVO;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface BusGcGameVORepository extends JpaRepository<BusGcGameVO, Long> {

    @Query("""
    FROM BusGcGameVO a
    LEFT JOIN BusGcGameType b ON a.id = b.gameId
    WHERE
    (:#{#param.zhName} IS NULL OR a.zhName = :#{#param.zhName})
    AND (:#{#param.enName} IS NULL OR a.enName = :#{#param.enName})
    AND (:#{#param.zhDescription} IS NULL OR a.zhDescription = :#{#param.zhDescription})
    AND (:#{#param.enDescription} IS NULL OR a.enDescription = :#{#param.enDescription})
    AND (:#{#param.typeId} IS NULL OR b.typeId = :#{#param.typeId})
    """)
    List<BusGcGameVO> getBodyList(@Param("param") BusGcGameSearchDTO param);


    @Query("""
    FROM BusGcGameVO a
    LEFT JOIN BusGcGameType b ON a.id = b.gameId
    WHERE
    (:#{#param.zhName} IS NULL OR :#{#param.zhName} = '' OR a.zhName like CONCAT('%',:#{#param.zhName},'%'))
    AND (:#{#param.enName} IS NULL OR :#{#param.enName} = '' OR a.enName like CONCAT('%',:#{#param.enName},'%'))
    AND (:#{#param.zhDescription} IS NULL OR :#{#param.zhDescription} = '' OR a.zhDescription like CONCAT('%',:#{#param.zhDescription},'%'))
    AND (:#{#param.enDescription} IS NULL OR :#{#param.enDescription} = '' OR a.enDescription like CONCAT('%',:#{#param.enDescription},'%'))
    AND (:#{#param.typeId} IS NULL OR :#{#param.typeId} = '' OR b.typeId = :#{#param.typeId})
    ORDER BY a.clickCount DESC
    """)
    Page<BusGcGameVO> getBodyData(@Param("param") BusGcGameSearchDTO param, Pageable pageable);

    @Query("""
    FROM BusGcGameVO a
    WHERE
    (:#{#param.zhName} IS NULL OR :#{#param.zhName} = '' OR a.zhName like CONCAT('%',:#{#param.zhName},'%'))
    AND (:#{#param.enName} IS NULL OR :#{#param.enName} = '' OR a.enName like CONCAT('%',:#{#param.enName},'%'))
    AND (:#{#param.zhDescription} IS NULL OR :#{#param.zhDescription} = '' OR a.zhDescription like CONCAT('%',:#{#param.zhDescription},'%'))
    AND (:#{#param.enDescription} IS NULL OR :#{#param.enDescription} = '' OR a.enDescription like CONCAT('%',:#{#param.enDescription},'%'))
    ORDER BY a.clickCount DESC
    """)
    Page<BusGcGameVO> getBodyDataByTypeIdIsNull(@Param("param") BusGcGameSearchDTO param, Pageable pageable);

    /**
     * 获取点击数前12的游戏
     */
    @Query("""
    FROM BusGcGameVO a
    ORDER BY a.clickCount DESC
    """)
    List<BusGcGameVO> getTop12Game(Pageable pageable);

    /**
     * 模糊查询游戏
     */
    @Query("""
            SELECT t FROM BusGcGameVO t
            LEFT JOIN BusGcGameI18n b ON t.id = b.gameId
            WHERE
            (:#{#param.keyword} IS NULL OR :#{#param.keyword} = '' OR b.name like CONCAT('%',:#{#param.keyword},'%'))
   
            ORDER BY t.gmtModified DESC
            """)
    Page<BusGcGameVO> getLimitGame(@Param("param") BusGcGameSearchDTO param, Pageable pageable);


}
